{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-03-20T10:03:23.921650Z",
     "start_time": "2020-03-20T10:03:23.915666Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import json\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence\n",
    "from vocab import Vocabulary, deserialize_vocab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-03-20T09:55:56.666614Z",
     "start_time": "2020-03-20T09:55:56.662623Z"
    }
   },
   "outputs": [],
   "source": [
    "vocab_path = \"./vocab/\"\n",
    "data_name = \"coco_precomp\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-03-20T09:55:57.122761Z",
     "start_time": "2020-03-20T09:55:57.103715Z"
    }
   },
   "outputs": [],
   "source": [
    "vocab = deserialize_vocab(os.path.join(vocab_path, '%s_vocab.json' % data_name)) # coco_precomp\n",
    "vocab_size = len(vocab)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-03-20T09:56:35.693624Z",
     "start_time": "2020-03-20T09:56:31.423015Z"
    }
   },
   "outputs": [],
   "source": [
    "with open(\"./dataset.json\", \"r\") as  f:\n",
    "    data = json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-03-20T10:00:13.353355Z",
     "start_time": "2020-03-20T10:00:13.348370Z"
    }
   },
   "outputs": [],
   "source": [
    "tokens = data[\"images\"][0][\"sentences\"][0][\"tokens\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-03-20T10:02:01.444649Z",
     "start_time": "2020-03-20T10:02:01.424695Z"
    }
   },
   "outputs": [],
   "source": [
    "# tokens = nltk.tokenize.word_tokenize(\n",
    "#     str(sent).lower())\n",
    "caption = []\n",
    "caption.append(vocab('<start>'))\n",
    "caption.extend([vocab(token) for token in tokens])\n",
    "caption.append(vocab('<end>'))\n",
    "target = torch.Tensor(caption)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-03-20T10:02:10.519372Z",
     "start_time": "2020-03-20T10:02:10.505414Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1.0000e+00, 9.5660e+03, 7.2350e+03, 3.5400e+02, 9.5660e+03, 6.3620e+03,\n",
       "        5.3110e+03, 8.5100e+02, 9.5660e+03, 3.7300e+02, 8.7980e+03, 8.5100e+02,\n",
       "        9.5660e+03, 3.9580e+03, 1.0207e+04, 2.0000e+00])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-03-20T10:03:34.870738Z",
     "start_time": "2020-03-20T10:03:34.777942Z"
    }
   },
   "outputs": [],
   "source": [
    "word_dim = 300\n",
    "embed = nn.Embedding(vocab_size, word_dim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-03-20T10:16:57.784324Z",
     "start_time": "2020-03-20T10:16:57.768367Z"
    }
   },
   "outputs": [],
   "source": [
    "x = embed(target.long())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-03-20T10:20:56.675587Z",
     "start_time": "2020-03-20T10:20:56.664617Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[-0.3909,  0.6435, -0.3526,  ...,  1.9630, -0.5507, -0.8850],\n",
       "         [-0.1843,  1.8298,  1.7251,  ..., -0.8283, -1.0117, -0.0944],\n",
       "         [-0.0178, -0.8504,  0.2214,  ...,  0.0593, -2.1379,  2.0280],\n",
       "         ...,\n",
       "         [ 0.1158,  1.0261,  0.5766,  ...,  1.6575,  1.9351,  0.4099],\n",
       "         [ 0.3569,  1.0874,  1.5907,  ...,  0.7927,  0.4499, -0.7405],\n",
       "         [-0.1998,  2.0272, -1.8024,  ..., -0.0680, -0.0106,  0.7359]]],\n",
       "       grad_fn=<UnsqueezeBackward1>)"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.unsqueeze_(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-03-20T10:21:03.969086Z",
     "start_time": "2020-03-20T10:21:03.962106Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 16, 300])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-03-20T10:21:56.972331Z",
     "start_time": "2020-03-20T10:21:56.968338Z"
    }
   },
   "outputs": [],
   "source": [
    "y = pack_padded_sequence(x, [16], batch_first=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-03-20T10:22:03.065032Z",
     "start_time": "2020-03-20T10:22:03.058050Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([16, 300])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.data.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
